package ymy.common;

import ymy.GeneralConfig;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class RoutePoint {
    private int id;
    private Coordinate position;

    // directionRoadMap用于保存当前路由点通过不同的行驶方向可达的道路映射表
    private Map<Integer, Road> directionRoadMap;

    private Random random = null;

    public RoutePoint(int id) {
        this.id = id;
        this.directionRoadMap = new HashMap<>();
        this.random = new Random(System.currentTimeMillis());
    }

    public int getId() {
        return id;
    }

    public Coordinate getPosition() {
        return position;
    }

    public void setPosition(Coordinate position) {
        this.position = position;
    }

    public int nextRandomDirection(int currDirection) {
        int sval = this.random.nextInt(4) + 1;

        if (this.directionRoadMap.containsKey(sval))
            return sval;

        return GeneralConfig.UNKNOWN;
    }

    public int nextTurnLeftDirection(int currDirection) {
        int sval = GeneralConfig.UNKNOWN;
        if (currDirection == GeneralConfig.NORTH)
            sval = GeneralConfig.WEST;
        if (currDirection == GeneralConfig.WEST)
            sval = GeneralConfig.SOUTH;
        if (currDirection == GeneralConfig.SOUTH)
            sval = GeneralConfig.EAST;
        if (currDirection == GeneralConfig.EAST)
            sval = GeneralConfig.NORTH;

        if (this.directionRoadMap.containsKey(sval))
            return sval;

        return GeneralConfig.UNKNOWN;
    }

    public int nextTurnRightDirection(int currDirection) {
        int sval = GeneralConfig.UNKNOWN;
        if (currDirection == GeneralConfig.NORTH)
            sval = GeneralConfig.EAST;
        if (currDirection == GeneralConfig.EAST)
            sval = GeneralConfig.SOUTH;
        if (currDirection == GeneralConfig.SOUTH)
            sval = GeneralConfig.WEST;
        if (currDirection == GeneralConfig.WEST)
            sval = GeneralConfig.NORTH;

        if (this.directionRoadMap.containsKey(sval))
            return sval;

        return GeneralConfig.UNKNOWN;
    }

    public int nextGoStraightDirection(int currDirection) {
        if (this.directionRoadMap.containsKey(currDirection))
            return currDirection;

        return GeneralConfig.UNKNOWN;
    }

    public void addDirectionRoad(int direct, Road road) {
        this.directionRoadMap.put(direct, road);
    }

    public void delDirectionRoad(int direct) {
        this.directionRoadMap.remove(direct);
    }
}